package com.jixiantubu.wode;

import image.SmartImageView;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.Wo_Adapter;
import com.jixiantubu.bean.UserBean;
import com.jixiantubu.bean.Wo_Data;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class WoFragment extends Fragment {
	View woView;
	ListView listview;
	Wo_Adapter adapter;
	List<Wo_Data> list = new ArrayList<Wo_Data>();
	SmartImageView photo;
	TextView nicheng;
	HttpUtils httpUtils;
	List<UserBean> list1 = new ArrayList<UserBean>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initData();
	}
	private void initData() {
			Wo_Data d1 = new Wo_Data(R.drawable.wodetiezi, "我的帖子");
		Wo_Data d2 = new Wo_Data(R.drawable.wodeyouji, "我的游记");
		Wo_Data d3 = new Wo_Data(R.drawable.wodeshoucang, "我的收藏");
		Wo_Data d4 = new Wo_Data(R.drawable.wodeguanzhu, "我的关注");
		Wo_Data d5 = new Wo_Data(R.drawable.wodejieban, "我的结伴");	
		list.add(d1);
		list.add(d2);
		list.add(d3);
		list.add(d4);
		list.add(d5);
		adapter = new Wo_Adapter(list, getActivity());
		// 向服务器发送请求，查询数据
		httpUtils = new HttpUtils();
		String url = "http://" + MyApplication.getIP()
				+ ":8080/Jinnang/WoGuanzhu";
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", "2");
		params.addBodyParameter("username", MyApplication.username);
		System.out.println("传递的username" + MyApplication.username);
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// 获取服务器端返回的数据结果
						String result = responseInfo.result;
						 Log.e("--------周星驰------", result);
						// shiyongGson框架进行json解析
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<UserBean>>() {
						}.getType();
						List<UserBean> list2 = gson.fromJson(result, typeOfT);
						list1.addAll(list2);
						if(list1.get(0).getUser_nickname()!=null){
							nicheng.setText(list1.get(0).getUser_nickname());
							}else{
								nicheng.setText("点击登录");
							}
						String url = "http://" + MyApplication.getIP()
								+ ":8080/Jinnang/img/"
								+ list1.get(0).getUser_imgPath();
						photo.setstyle("circle");
						photo.setImageUrl(url);
						 Log.e("--------周润发------", url);
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// TODO Auto-generated method stub

					}
				});

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		woView = inflater.inflate(R.layout.wo, null);
		initViews();
		return woView;
	}

	private void initViews() {
		nicheng = (TextView) woView.findViewById(R.id.wo_nickname);
		listview = (ListView) woView.findViewById(R.id.wo_lv1);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				switch (arg2) {
				case 0:
					Intent intent0 = new Intent(getActivity(),
							Wo_tieziActivity.class);
					intent0.putExtra("username", MyApplication.username);
				Log.e(">>>>>>>>>>", MyApplication.username);
					startActivity(intent0);
					break;
				case 1:
					Intent intent1 = new Intent(getActivity(),
							Wo_youjiActivity.class);
					startActivity(intent1);

					break;
				case 2:
					Intent intent2 = new Intent(getActivity(),
							Wo_shoucangActivity.class);
					intent2.putExtra("username", MyApplication.username);
					startActivity(intent2);

					break;
				case 3:
					Intent intent3 = new Intent(getActivity(),
							Wo_guanzhuActivity.class);
					intent3.putExtra("username", MyApplication.username);
					startActivity(intent3);
					break;
				case 4:
					Intent intent4 = new Intent(getActivity(),
							Wo_jiebanActivity.class);
					intent4.putExtra("username", MyApplication.username);
					startActivity(intent4);

					break;
				default:
					break;
				}

			}

		});

		photo = (SmartImageView) woView.findViewById(R.id.wo_imagebt1);

		photo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(getActivity(),
						Wo_ziliaoActivity.class);
				startActivity(intent);
				// }

			}
		});

	}

}
